package scheduler;

import javax.microedition.lcdui.*;

import core.*;

import commands.Commands;
import commands.DaySelectedCommand;
import commands.Dispatcher;
import commands.NewEntryCommand;
import controllers.ScheduleController;



public class AddForm extends Form  implements CommandListener {
        
        private TextField m_pairNumField;
        private TextField m_subjNameField;
        private ChoiceGroup m_subjTypeChoice;
        private TextField m_teachersNameField; 
        private TextField m_roomField; 
        private TextField m_comentsField; 
       // private ChoiceGroup m_weekTypeChoice; 
        
        private static final Command OkCmd = new Command(Commands.Ok, Command.OK, 1);
        private static final Command BackCmd = new Command(Commands.Back, Command.BACK, 1);
        
        private static final String FormName = "Add pair"; 
        
        private DayOfWeek m_day;
        private WeekDefinition m_week;
        
        public AddForm(){
                
                super(FormName);
                
                m_pairNumField = new TextField("Number:", null, 1, TextField.NUMERIC);
                append(m_pairNumField);
                m_subjNameField = new TextField("Subj Name:", null, 15, TextField.ANY);
                append(m_subjNameField);
                m_subjTypeChoice = new ChoiceGroup("Type:", Choice.EXCLUSIVE, DisciplineType.getNames(), null);
                append(m_subjTypeChoice);
                m_teachersNameField = new TextField("Teachers Name:", null, 15, TextField.ANY);
                append(m_teachersNameField);
                m_roomField = new TextField("Number of the Room:", null, 15, TextField.ANY);
                append(m_roomField);
                m_comentsField = new TextField("Comments:", null, 15, TextField.ANY);
                append(m_comentsField);
                /*m_weekTypeChoice = new ChoiceGroup("", Choice.EXCLUSIVE, WeekDefinition.getWeeeksNames(), null);
                append(m_weekTypeChoice);*/
                
                addCommand(OkCmd);
                addCommand(BackCmd);
                setCommandListener(this);
                
                Dispatcher.getInstance().registerListener(this, Commands.DaySelected);
        }

        public void commandAction(Command cmd, Displayable arg1) {
                switch(cmd.getCommandType()){
                case Command.OK :
                        onOk();
                        break;
                case Command.BACK :
                        Dispatcher.getInstance().handleCommand(new Command(Commands.Cancel, Command.ITEM, 1));
                        break;
                case Command.ITEM :
                        if (cmd.getLabel().equals(Commands.DaySelected)){
                                DaySelectedCommand daySelectedCmd = (DaySelectedCommand)cmd;
                                daySelected(daySelectedCmd.getWeek(), daySelectedCmd.getDay());
                        }
                }
        }
        
        private void daySelected(WeekDefinition week, DayOfWeek day){           
                m_day = day;
                m_week = week;          
                //m_weekTypeChoice.setSelectedIndex(m_week.getWeekNumber(), true);
        }
                
        private void onOk()
        {
                //WeekDefinition week = new WeekDefinition(m_weekTypeChoice.getSelectedIndex());
                TimeOfTheClasses time = new TimeOfTheClasses(Integer.parseInt(m_pairNumField.getString()));
                Discipline disc = new Discipline(m_subjNameField.getString(), new DisciplineType(m_subjTypeChoice.getSelectedIndex())); 
                Subject subj = new Subject(disc, m_roomField.getString(), m_teachersNameField.getString(), m_comentsField.getString()); 
                ScheduleEntry resEntry = new ScheduleEntry(m_day, m_week, time, subj);
                                
                if(!ScheduleController.getInstance().getSchedule().isTimeBusy(m_day, m_week, time)){
                        Dispatcher.getInstance().handleCommand(new NewEntryCommand(resEntry));
                }
                else{
                        Alert m_alert = new Alert("Warning", "This pair has already existed!!!", null, AlertType.WARNING);
                        m_alert.setTimeout(Alert.FOREVER);
                }
        }
}